home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / shadingwinds / shadingwinds.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  14.7 KB  |  652 lines

  1. /*
  2.     File:        ShadingWinds.c
  3.  
  4.     Contains:    This little application includes code to allow you to detect whether or not the Shell
  5.                 Window is "rolled up" by WindowShade.  How do you do this?  You check the window's 
  6.                 contRgn.  If the contRgn is empty, then the window is shaded.  You'll note that the
  7.                 grafPort is unchanged by WindowShade, so if you need to save window dimensions, you
  8.                 can grab that information from the portRect.
  9.  
  10.     Written by:  Virginia (Ginny) McCulloh    
  11.  
  12.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  13.  
  14.                 You may incorporate this Apple sample source code into your program(s) without
  15.                 restriction. This Apple sample source code has been provided "AS IS" and the
  16.                 responsibility for its operation is yours. You are not permitted to redistribute
  17.                 this Apple sample source code as "Apple sample source code" after having made
  18.                 changes. If you're going to re-distribute the source, we require that you make
  19.                 it clear in the source that the code was descended from Apple sample source
  20.                 code, but that you've made changes.
  21.  
  22.     Change History (most recent first):
  23.                 8/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  24.                 
  25.  
  26. */
  27.  
  28. #include "ShadingWinds.h"
  29. #include <TextUtils.h>
  30.  
  31. typedef struct {
  32.     Rect     windPort;
  33.     Point     windPosition;
  34. }WindDimAndPos;
  35.  
  36.  
  37. /*****     Prototypes      *****/
  38. void        main(void);
  39. void         initApp(void);
  40. Boolean        checkGestaltFeatures(void);
  41. Boolean     installAEHandlers();
  42. void         setUpMenus(void);
  43. void         setUpWindow(void);
  44. void         setUpMenus(void);
  45. void         doHighLevel(EventRecord *eventRec);
  46. void         doMousedown(EventRecord *eventRec);
  47. void         doKey(EventRecord *eventRec);
  48. void        doUpdate(EventRecord *eventRec);
  49. void         dispatch(long menuResult);
  50. void         doAppleCmds (short theItem);
  51. void         doFileCmds (short theItem);
  52. void         doTestCmds (short theItem);
  53. void         TestForWindowShade(void);
  54. void         HideShellWindow(void);
  55. void         RevealShellWindow(void);
  56.  
  57.  
  58. pascal OSErr    DoOpenAppAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon);
  59. pascal OSErr    DoOpenDocAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon);
  60. pascal OSErr    DoPrintDocAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon);
  61. pascal OSErr    DoQuitAppAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon);
  62.  
  63.  
  64. /*****  globals *****/
  65. Boolean            gQuitTheApp = false;        /* true = quit program */
  66. WindowPtr        gStatusWindow, gHiddenWindow;
  67. Boolean            gIsShellWindVisible = false;
  68. WindDimAndPos    myWindDimAndPos;
  69.  
  70. /*****    main() *****/
  71.  
  72. void main()
  73. {
  74.     short            sleepTime  =  60;
  75.     EventRecord        myEvent;
  76.  
  77.     
  78.     initApp();                                /* call initialization routine */
  79.     setUpMenus();
  80.     do
  81.     {
  82.         if (WaitNextEvent(everyEvent, &myEvent, sleepTime, NIL))
  83.             switch (myEvent.what)
  84.             {
  85.                 case mouseDown:
  86.                     doMousedown(&myEvent);
  87.                     break;
  88.                     
  89.                 case keyDown:
  90.                 case autoKey:
  91.                     doKey(&myEvent);
  92.                     break;
  93.  
  94.                 case kHighLevelEvent:
  95.                     doHighLevel(&myEvent);
  96.                     break;
  97.  
  98.                 case activateEvt:
  99.                     break;
  100.                 
  101.                 case updateEvt:
  102.                     doUpdate(&myEvent);
  103.                     break; 
  104.                 
  105.                 case diskEvt:
  106.                     break;
  107.             
  108.                 case osEvt:                /*  I don't really care yet. */
  109.                     break;
  110.             }
  111.     }  while (!gQuitTheApp);        
  112.     
  113. }                 /* end of main */
  114.  
  115.  
  116. /*****    initApp() *****/
  117.  
  118. void initApp()
  119. {
  120.     
  121.     /* Memory specific initializations. */
  122.     
  123.     MaxApplZone();                /* grow the heap to its maximum size */
  124.     MoreMasters();                   /* create more master pointers         */
  125.     MoreMasters();
  126.     MoreMasters();
  127.     MoreMasters();
  128.     MoreMasters();
  129.  
  130.     /* Initializing the ROM Managers */
  131.     
  132.     InitGraf((Ptr) &qd.thePort);
  133.     InitFonts();
  134.     InitWindows();
  135.     InitMenus();
  136.     FlushEvents(everyEvent,0);
  137.     TEInit();
  138.     InitDialogs(NIL);
  139.     InitCursor();
  140.     
  141.     if (!checkGestaltFeatures())
  142.         ExitToShell();
  143.     
  144.     /* Install the required Apple Event Handlers */
  145.     if (!installAEHandlers())
  146.         ExitToShell();
  147. }                /* end of initApp() */
  148.  
  149.  
  150.  
  151. /*****  checkGestaltFeatures() *****/
  152. Boolean checkGestaltFeatures()
  153. {
  154.     long        gestaltFeature;
  155.     OSErr        myErr;
  156.  
  157.     myErr = Gestalt(gestaltSystemVersion, &gestaltFeature);     /* which SysVersion present? */
  158.     if (myErr == noErr)
  159.     {
  160.         gestaltFeature = (gestaltFeature >> 8) & 0xf; 
  161.                                         /* shift result over & mask out major version number */
  162.         if (gestaltFeature < 7)         /* This is a System 7+ shell.  We quit otherwise. */
  163.         {
  164.             StopAlert(BADSYSTEMID, nil);
  165.             return(false);
  166.         }
  167.     }
  168.     
  169.     if (myErr == noErr)
  170.     {
  171.         myErr = Gestalt(gestaltQuickdrawVersion, &gestaltFeature);
  172.                                                     /* we want color QD cuz we're spoiled */
  173.         if (myErr == noErr)
  174.         {
  175.             if(gestaltFeature < gestalt32BitQD)
  176.             {
  177.                 StopAlert(BADQUICKDRAWID, nil);
  178.                 return(false);
  179.             }
  180.         }
  181.     }
  182.     
  183.     
  184.     
  185.     if (myErr == noErr)
  186.     {
  187.         myErr = Gestalt(gestaltAppleEventsAttr, &gestaltFeature);
  188.         if (myErr == noErr)
  189.         {
  190.             if (!(gestaltFeature & 0xf))
  191.             {
  192.                 StopAlert(NOAPPLEEVENTS, nil);
  193.                 return(false);
  194.             }
  195.         }
  196.     }
  197.     if (!myErr)        
  198.         return(true);            /* if there was an error we cannot continue */
  199.     else
  200.         return(false);            /* we made it through without a hitch */
  201. }
  202.  
  203. /*****    installAEHandlers *****/
  204. Boolean installAEHandlers()
  205. {
  206.     OSErr        myErr;
  207.     
  208.     myErr = AEInstallEventHandler ( kCoreEventClass, 
  209.                     kAEOpenApplication, NewAEEventHandlerProc(DoOpenAppAE), 0L, false );
  210.     if (myErr == noErr)
  211.         myErr = AEInstallEventHandler ( kCoreEventClass,
  212.                     kAEOpenDocuments, NewAEEventHandlerProc(DoOpenDocAE), 0L, false );
  213.     if (myErr == noErr)
  214.         myErr = AEInstallEventHandler ( kCoreEventClass,
  215.                     kAEPrintDocuments, NewAEEventHandlerProc(DoPrintDocAE), 0L, false );
  216.     if (myErr == noErr)
  217.         myErr = AEInstallEventHandler ( kCoreEventClass,
  218.                     kAEQuitApplication, NewAEEventHandlerProc(DoQuitAppAE), 0L, false );
  219.     if (myErr)
  220.         return(false);
  221.     else
  222.         return(true);
  223. }
  224.  
  225. /*****    setUpMenus() 
  226.             This will put up our menus.  Be sure to read the comments by the
  227.             AppendMenu() and SetItem() calls to see how different options
  228.             behave.        
  229. *****/
  230. void setUpMenus()
  231. {
  232.     short            n;
  233.     MenuHandle        theMenu;
  234.     
  235.     for (n = 0; n < MAXMENUS; n++)
  236.     {
  237.         theMenu = GetMenu(FIRSTMENUID + n);
  238.         if (theMenu)                  
  239.             InsertMenu(theMenu, 0);
  240.     }
  241.  
  242.     theMenu = GetMenuHandle(APPLEMENU);
  243.     if (theMenu)
  244.         AppendResMenu(theMenu, 'DRVR');
  245.     
  246.     DrawMenuBar();
  247.     return;
  248. }                    /* end of setUpMenus()  */
  249.  
  250. /*****    setUpWindow() *****/
  251.  
  252. void setUpWindow( void )
  253. {
  254. #define        GLOBALWINDID    2001
  255.  
  256.     WindowPtr    myWind;
  257.  
  258.     myWind = GetNewCWindow(WINDOWID, nil, PUTINFRONT);
  259.     if (myWind != nil)
  260.     {
  261.         ShowWindow(myWind);
  262.         gIsShellWindVisible = true;
  263.     }
  264.     gStatusWindow = GetNewWindow(GLOBALWINDID, nil, PUTINFRONT);
  265.     if (gStatusWindow == nil)
  266.         DebugStr("\pWe can't put up StatusWindow.");
  267.     
  268.     SelectWindow(myWind);        // activate the "Shell Window"
  269. }
  270.  
  271.  
  272. /*****    doMousedown()
  273.             We figure out where the user has clicked the mouse.  If the user
  274.             clicks anywhere but the menu bar, we beep.  Otherwise, we figure
  275.             out which menu they have clicked and dispatch.
  276. *****/
  277. void doMousedown(EventRecord *eventRec)
  278. {
  279.     short        windPart;
  280.     WindowPtr    myWind;
  281.     long        menuResult;
  282.     
  283.     windPart = FindWindow(eventRec->where, &myWind);
  284.     
  285.     switch(windPart)
  286.     {
  287.         case inContent:
  288.             SelectWindow(myWind);
  289.             break;
  290.  
  291.         case inDrag:
  292.             DragWindow( myWind, eventRec->where, &qd.screenBits.bounds );
  293.             break;
  294.  
  295.         case inMenuBar:
  296.             menuResult = MenuSelect(eventRec->where);
  297.             dispatch(menuResult);
  298.             break;
  299.  
  300.         case inSysWindow:
  301.             SystemClick( eventRec, myWind );
  302.             break;
  303.             
  304.         case inGoAway:
  305.             gQuitTheApp = true;
  306.             break;
  307.             
  308.         default:
  309.             break;
  310.     }
  311.     return;
  312. }                 /* end of doMousedown */
  313.  
  314.  
  315. /*****     doKey()
  316.             We ignore keys pressed unless they are accompanied by a 
  317.             command key.  Then we dispatch.
  318. *****/
  319. void doKey(EventRecord *eventRec)
  320. {
  321.     char    keyPressed;
  322.     long    menuResult;
  323.     
  324.     keyPressed = (char) (eventRec->message & charCodeMask);
  325.     
  326.     if((eventRec->modifiers & cmdKey) != 0)
  327.     {
  328.         menuResult = MenuKey(keyPressed);
  329.         dispatch(menuResult);    
  330.     }
  331.     return;
  332. }                 /* end of do_key */
  333.  
  334.  
  335. /*****    doHighLevel() *****/
  336. void doHighLevel(EventRecord *eventRec)
  337. {
  338.     OSErr myErr;
  339.     myErr = AEProcessAppleEvent(eventRec);
  340. }
  341.  
  342.  
  343. pascal OSErr    DoOpenAppAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  344. {
  345.     #pragma unused(theAppleEvent,reply,refCon)
  346.     setUpWindow();
  347.     return(noErr);
  348. }
  349. pascal OSErr    DoOpenDocAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  350. {
  351.     #pragma unused(theAppleEvent,reply,refCon)
  352.     return(noErr);
  353. }
  354. pascal OSErr    DoPrintDocAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  355. {
  356.     #pragma unused(theAppleEvent,reply,refCon)
  357.     return(noErr);
  358. }
  359. pascal OSErr    DoQuitAppAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  360. {
  361.     #pragma unused(theAppleEvent,reply,refCon)
  362.     gQuitTheApp = true;
  363.     return(noErr);
  364. }
  365.  
  366.  
  367. /***** doUpdate() *****/
  368. void    doUpdate(EventRecord *eventRec)
  369. {
  370.     GrafPtr        savedPort;
  371.     WindowPtr    theWindow;
  372.     theWindow = (WindowPtr) eventRec->message;
  373.     
  374.     GetPort(&savedPort);
  375.     SetPort((GrafPtr) theWindow);
  376.     BeginUpdate(theWindow);
  377.     EndUpdate(theWindow);
  378.     SetPort(savedPort);
  379. }
  380.  
  381.  
  382. /*****     dispatch()
  383.             We determine which menu the user has chosen (either with mouse
  384.             or with command keys) and jump to the routine that handles
  385.             that menu's commands.
  386. *****/
  387. void dispatch(long menuResult)
  388. {
  389.     short        theMenu;            /* menu selected */
  390.     short        theItem;            /* item selected */
  391.     
  392.     theMenu = HiWord (menuResult);        /* menuID selected */
  393.     theItem = LoWord (menuResult);        /* item# selected */
  394.     
  395.     switch (theMenu)
  396.     {
  397.         case APPLEMENU:
  398.             doAppleCmds(theItem);
  399.             break;
  400.  
  401.         case FILEMENU:
  402.             doFileCmds(theItem);
  403.             break;
  404.             
  405.         case EDITMENU:
  406.             break;
  407.  
  408.         case TESTMENU:
  409.             doTestCmds(theItem);
  410.             break;
  411.     }
  412.     HiliteMenu(0);
  413. }                /* end of dispatch */
  414.  
  415.  
  416.  
  417. /*****     doAppleCmds() 
  418.             When the user chooses the "About MyMenuText" item, we display the
  419.             About box.  If the user chooses a DA, we open the DA.
  420. *****/
  421. void doAppleCmds(short theItem)
  422. {    
  423.     MenuHandle        myMenu;
  424.     Str255            name;
  425.     short            dummy;
  426.     
  427.     if(theItem == appleABOUT)
  428.     {
  429.         Alert(ABOUTID, (ModalFilterUPP) NIL);
  430.     }
  431.     else
  432.     {
  433.         myMenu = GetMenuHandle (APPLEMENU);
  434.         if (myMenu)
  435.         {
  436.             GetMenuItemText(myMenu, theItem, name);
  437.             dummy = OpenDeskAcc(name);
  438.         }
  439.     }
  440.     return;
  441. }                 /* end of doAppleCmds */
  442.  
  443.  
  444.  
  445. /*****    doFileCmds() 
  446.             When the user chooses Quit on the File menu, we quit.
  447. *****/
  448. void doFileCmds (short theItem)
  449. {
  450.     switch (theItem)
  451.     {            
  452.         case fileQUIT:
  453.             gQuitTheApp = true;
  454.             break;
  455.     }
  456.     return;
  457. } /* end of doFileCmds */
  458.  
  459.  
  460.  
  461. /*****    doTestCmds()
  462.             When the user chooses any of the items in the Test menu.  
  463.             They will see the TESTALERT. 
  464. *****/
  465. void doTestCmds (short theItem)
  466. {    
  467.  
  468.     switch (theItem)
  469.     {
  470.         case testWINDSHADE:
  471.             TestForWindowShade();
  472.             break;
  473.         case testHIDEWIND:
  474.             HideShellWindow();
  475.             break;
  476.         case testREVEALWIND:
  477.             RevealShellWindow();
  478.             break;
  479.     }    
  480.     return;
  481. }                /* end of doTestCmds */
  482.  
  483.  
  484.  
  485. void TestForWindowShade()
  486. {
  487.     WindowPtr    myShellWindow;
  488.     GrafPtr        oldPort;
  489.     Str255        theString;
  490.     short        windDimensions;
  491.     Point        globalWindPosition;
  492.     
  493.     myShellWindow = FrontWindow();        /* 
  494.                                             Unless you click in the StatusWindow, your
  495.                                             front window should be the Shell Window.
  496.                                         */
  497.     if (myShellWindow)        // if myShellWindow is not nil
  498.     {
  499.         if (myShellWindow != gStatusWindow)    // Make sure we're looking at Shell Window
  500.         {
  501.             GetPort(&oldPort);
  502.             SetPort(gStatusWindow);
  503.                 EraseRect(&gStatusWindow->portRect);
  504.                 MoveTo(10, 20);
  505.                 DrawString("\pShell Window's dimensions:");
  506.                 MoveTo(70, 40);
  507.                 DrawString("\pWidth = ");
  508.                 MoveTo(150, 40);
  509.                 windDimensions = (*myShellWindow).portRect.right - 
  510.                             (*myShellWindow).portRect.left;
  511.                 NumToString((long) windDimensions, theString);
  512.                 DrawString(theString);
  513.                 MoveTo(70, 60);
  514.                 DrawString("\pHeight = ");
  515.                 MoveTo(150, 60);
  516.                 windDimensions = (*myShellWindow).portRect.bottom - 
  517.                             (*myShellWindow).portRect.top;
  518.                 NumToString((long) windDimensions, theString);
  519.                 DrawString(theString);
  520.                 
  521.                 MoveTo(10, 80);
  522.                 DrawString ("\pThe global top and left of content region are:");
  523.                 
  524.                 MoveTo(70, 100);
  525.                 DrawString("\pGlobal Top = ");
  526.                 
  527.                 MoveTo(150, 100);
  528.                 globalWindPosition.v = (**((WindowPeek)myShellWindow)->contRgn).rgnBBox.top;
  529.                 NumToString((long) globalWindPosition.v, theString);
  530.                 DrawString(theString);
  531.                 
  532.                 MoveTo(70, 120);
  533.                 DrawString("\pGlobal Left = ");
  534.                 
  535.                 MoveTo(150, 120);
  536.                 globalWindPosition.h = (**((WindowPeek)myShellWindow)->contRgn).rgnBBox.left;
  537.                 NumToString((long) globalWindPosition.h, theString);
  538.                 DrawString(theString);
  539.                 
  540.                 MoveTo(10, 140);
  541.                 
  542.                 /*
  543.                     The next section of code tests to see if myShellWindow's
  544.                     contRgn is EMPTY. If it is, that means that WindowShade 
  545.                     has rolled up the content region.
  546.                     Note:  the window's portRect dimensions do not change.
  547.                 */
  548.                 
  549.                 if (EmptyRgn(((WindowPeek) myShellWindow)->contRgn))
  550.                     DrawString("\pShell Window has been rolled up.  Unshade it and retest.");
  551.                 else
  552.                     DrawString("\pShell Window unrolled. Shade the Shell Window & retest.");
  553.             SetPort(oldPort);
  554.         }
  555.         else        /*
  556.                         The status window is the front window.
  557.                         We don't test for shading there.  Tell user
  558.                         to activate the Shell window.
  559.                     */
  560.         {
  561.             GetPort(&oldPort);
  562.             SetPort(gStatusWindow);
  563.                 EraseRect(&gStatusWindow->portRect);
  564.                 MoveTo(10, 20);
  565.                 DrawString("\pWrong window is active. Click in Shell Window & retest.");
  566.             SetPort(oldPort);
  567.         }
  568.     }
  569.     
  570.  
  571. }
  572.  
  573. void HideShellWindow()
  574. {
  575.     WindowPtr    theShellWind;
  576.     GrafPtr        oldPort;
  577.     
  578.     
  579.     if (gIsShellWindVisible)
  580.     {
  581.         theShellWind = FrontWindow();
  582.         if (theShellWind != gStatusWindow)
  583.         {
  584.             gHiddenWindow = theShellWind;
  585.             myWindDimAndPos.windPort.left = (*theShellWind).portRect.left;
  586.             myWindDimAndPos.windPort.top = (*theShellWind).portRect.top;
  587.             myWindDimAndPos.windPort.right = (*theShellWind).portRect.right;
  588.             myWindDimAndPos.windPort.bottom = (*theShellWind).portRect.bottom;
  589.             
  590.             
  591.             // get the content rgn's global top and left coordinates
  592.             myWindDimAndPos.windPosition.h = (**(*(WindowPeek)theShellWind).contRgn).rgnBBox.left;
  593.             myWindDimAndPos.windPosition.v = (**(*(WindowPeek)theShellWind).contRgn).rgnBBox.top;
  594.                             
  595.             HideWindow(theShellWind);
  596.             gIsShellWindVisible = false;
  597.         }
  598.         else
  599.         {
  600.             GetPort(&oldPort);
  601.             SetPort(gStatusWindow);
  602.                 EraseRect(&gStatusWindow->portRect);
  603.                 MoveTo(10, 20);
  604.                 DrawString("\pWhy would you want to hide Status window?");
  605.             SetPort(oldPort);
  606.         }
  607.     }
  608.     else
  609.     {
  610.         GetPort(&oldPort);
  611.         SetPort(gStatusWindow);
  612.             EraseRect(&gStatusWindow->portRect);
  613.             MoveTo(10, 20);
  614.             DrawString("\pShell Window is hidden. Try revealing it instead.");
  615.         SetPort(oldPort);
  616.     }
  617. }
  618.  
  619. void RevealShellWindow()
  620. {
  621.     GrafPtr        oldPort;
  622.     
  623.     if (!gIsShellWindVisible)
  624.     {
  625.         if (gHiddenWindow != nil)
  626.         {
  627.             ShowWindow(gHiddenWindow);
  628.             SelectWindow(gHiddenWindow);
  629.             
  630.             gIsShellWindVisible = true;
  631.         }
  632.         else
  633.         {
  634.             GetPort(&oldPort);
  635.             SetPort(gStatusWindow);
  636.                 EraseRect(&gStatusWindow->portRect);
  637.                 MoveTo(10, 20);
  638.                 DrawString("\pProblems have emerged.  Quit while you're behind.");
  639.             SetPort(oldPort);
  640.         }
  641.     }
  642.     else
  643.     {
  644.         GetPort(&oldPort);
  645.         SetPort(gStatusWindow);
  646.             EraseRect(&gStatusWindow->portRect);
  647.             MoveTo(10, 20);
  648.             DrawString("\pShell window is visible.  Try hiding it first.");
  649.         SetPort(oldPort);
  650.     }
  651.  
  652. }